package com.albert.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 异步线程:1.直接继承Thread类 2.实现接口Runnable 3.实现Callable接口,使用FutureTask启动  4. 线程池提交任务
 * 
 */

public class ThreadFour {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("开始输出");
//        第一种
//        Thread01 thread01 = new Thread01();
//        thread01.start();

//        第二种
//        Runnable01 runnable01 = new Runnable01();
//        Thread thread = new Thread(runnable01);
//        thread.start();

        FutureTask<String> integerFutureTask = new FutureTask<>(new Callable01());
        new Thread(integerFutureTask).start();
        //callable接口可以获取返回结果
        String i = integerFutureTask.get();
        System.out.println(i);
        System.out.println("输出结束");
    }


    //1.直接继承线程
    public static class Thread01 extends Thread {
        public void run() {
            System.out.println("线程号" + Thread.currentThread());
            System.out.println(10);
        }
    }
    //2.实现接口
    public static class Runnable01 implements Runnable {
        public void run() {
            System.out.println("线程号" + Thread.currentThread());
            System.out.println(10);
        }
    }
    //3.实现callable接口,可以获取返回结果
    public static class Callable01 implements Callable<String> {
        public String call() {
            System.out.println("线程号" + Thread.currentThread());
            System.out.println(10);
            return "返回结果";
        }
    }
}
